{% extends "templates/main_template.html.j2" %}

{% import "macros/user_name.html.j2" as USER_NAME %}

{% block title -%}
  Активные запросы · Farado
{%- endblock %}

{% set active_menu = 'active_issues' %}


{#----------------------------------------------------------------------------#}
{% block styles %}
  <link href="/static/css/page-toolbar.css" rel="stylesheet" type="text/css" />
{% endblock %}


{#----------------------------------------------------------------------------#}
{% block breadcrumb %}
  <ol class="breadcrumb">
    <li class="breadcrumb-item active" aria-current="page">Активные запросы</li>
  </ol>
{% endblock %}


{#----------------------------------------------------------------------------#}
{% block content %}
  <div class="row mt-3">
    <div class="col-12">
      <div class="accordion" id="accordion_messages">
        {% for current_user in project_manager.ordered_users() if not current_user.is_blocked %}
          {% set user_statistics_helper = project_manager.user_statistics_helper(current_user) %}
          {% set issues_in_progress = user_statistics_helper.issues_in_progress() %}
          {% set issues_in_progress_length = issues_in_progress|length %}
          <div class="accordion-item">
            {# Пользователь #}
              <h2 class="accordion-header"
                  id="panels_user_active_issues_header_{{ current_user.id }}">
                <button
                    class="accordion-button collapsed
                      {%- if 0 == issues_in_progress_length %} text-secondary
                      {%- elif 1 < issues_in_progress_length %} bg-warning-subtle
                      {%- else %} bg-success-subtle
                      {%- endif -%}"
                    type="button"
                    data-bs-toggle="collapse"
                    data-bs-target="#panels_issues_content_{{ current_user.id }}"
                    aria-expanded="true"
                    aria-controls="panels_issues_content_{{ current_user.id }}">
                  {{ USER_NAME.VALUE_WITH_ICON(current_user, user.id) }}
                  [{{ issues_in_progress_length }}]
                </button>
              </h2>
            {# /Пользователь #}

            {# Перечень активных задач очередного пользователя #}
              <div id="panels_issues_content_{{ current_user.id }}"
                  class="accordion-collapse collapse"
                  aria-labelledby="panels_user_active_issues_header_{{ current_user.id }}">
                <div class="accordion-body">
                  {% if issues_in_progress %}
                    <ul class="list-group">
                      {% for issue in issues_in_progress %}
                        {% set issue_kind = project_manager.issue_kind(issue.issue_kind_id) %}
                        {% set workflow = project_manager.workflow(issue_kind.workflow_id) %}
                        {% set parent_issue = project_manager.issue(issue.parent_id) %}
                        <li class="list-group-item">
                          <div class="d-flex w-100 justify-content-between">
                            <div class="mb-1">

                              {# Наименование родительского запроса #}
                                <p class="mb-1">
                                  {% if parent_issue %}
                                    <a href="/issues/issue/{{ parent_issue.id }}"
                                        class="parent-title link"
                                        title="{{ parent_issue.caption|e }}">
                                      #{{ parent_issue.id }}: {{ parent_issue.caption|e }}
                                    </a>
                                  {% else %}
                                    <span class="parent-title link">
                                      (Запрос верхнего уровня)
                                    </span>
                                  {% endif %}
                                </p>
                              {# /Наименование родительского запроса #}

                              {# Наименование целевого запроса #}
                                <a href="/issues/issue/{{ issue.id }}"
                                    title="{{ issue.caption|e }}">
                                  <i class="bi bi-card-heading"></i>
                                  {{ workflow.caption|e }}: {{ issue_kind.caption|e }}
                                  #{{ issue.id }}: {{ issue.caption|e }}
                                </a>
                              {# /Наименование целевого запроса #}

                            </div>
                          </div>

                          {# Прогресс запроса #}
                            <div class="col-12">
                              <div class="progress">
                                {% set progress = project_manager.progress_value(issue.id) %}
                                <div
                                    class="progress-bar
                                      {%- if 100 == progress %} bg-success
                                      {%- elif 40 > progress %} bg-warning
                                      {%- endif %}"
                                    role="progressbar"
                                    style="width: {{ progress }}%;"
                                    aria-valuenow="{{ progress }}"
                                    aria-valuemin="0"
                                    aria-valuemax="100">{{ progress }}%</div>
                              </div>
                            </div>
                          {# /Прогресс запроса #}

                          {# -- Перечень свойств запроса -- #}
                            <div class="col-12">
                              <table class="table table-sm table-borderless">
                                <tbody>

                                  {# Состояние запроса #}
                                    {% set state = project_manager.state(issue.state_id) %}
                                    {% if state %}
                                      <tr>
                                        <td style="width: 2%;">
                                          Состояние:
                                        </td>
                                        <td>
                                          {{ state.caption|e }}
                                        </td>
                                      </tr>
                                    {% endif %}
                                  {# /Состояние запроса #}

                                  {# Проект #}
                                    {% set project = project_manager.project(issue.project_id) %}
                                    {% if project %}
                                      <tr>
                                        <td>
                                          Проект:
                                        </td>
                                        <td>
                                          <a href="/projects/project/{{ project.id }}">
                                            {{ project.caption|e }}
                                          </a>
                                        </td>
                                      </tr>
                                    {% endif %}
                                  {# /Проект #}

                                  {# Версия #}
                                    {% set version = project_manager.version(issue.version_id) %}
                                    {% if version %}
                                      <tr>
                                        <td>
                                          Версия:
                                        </td>
                                        <td>
                                          <a href="/projects/version/{{ version.id }}">
                                            {{ version.caption|e }}
                                          </a>
                                        </td>
                                      </tr>
                                    {% endif %}
                                  {# /Версия #}

                                  {# Изменён #}
                                    {% set last_modify_user = project_manager.user(issue.last_modify_user_id()) %}
                                    {% if last_modify_user %}
                                      <tr>
                                        <td>
                                          Изменён:
                                        </td>
                                        <td>
                                          {{- issue.last_modified() -}}
                                          <a href="/users/user_statistics/{{ last_modify_user.id }}">
                                            {{ USER_NAME.VALUE_WITH_ICON(last_modify_user, user.id) }}
                                          </a>
                                        </td>
                                      </tr>
                                    {% endif %}
                                  {# /Изменён #}

                                  {# Создан #}
                                    {% set create_user = project_manager.user(issue.create_user_id()) %}
                                    {% if create_user %}
                                      <tr>
                                        <td>
                                          Создан:
                                        </td>
                                        <td>
                                          {{- issue.created() -}}
                                          <a href="/users/user_statistics/{{ create_user.id }}">
                                            {{ USER_NAME.VALUE_WITH_ICON(create_user, user.id) }}
                                          </a>
                                        </td>
                                      </tr>
                                    {% endif %}
                                  {# /Создан #}

                                </tbody>
                              </table>
                            </div>
                          {# -- /Перечень свойств запроса -- #}
                        </li>
                      {% endfor %}
                    </ul>
                  {% else %}
                    Активных запросов не найдено.
                  {% endif %}
                </div>
              </div>
            {# /Перечень активных задач очередного пользователя #}
          </div>
        {% endfor %}
      </div>
    </div>
  </div>
{% endblock content %}


{#----------------------------------------------------------------------------#}
{% block scripts %}
{% endblock %}
